import cv2
import numpy as np


def detect_glove_direction(image_path, left_template_path, right_template_path):
    # 读取待检测图像和模板图像
    image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
    left_template = cv2.imread(left_template_path, cv2.IMREAD_GRAYSCALE)
    right_template = cv2.imread(right_template_path, cv2.IMREAD_GRAYSCALE)

    # 进行模板匹配
    result_left = cv2.matchTemplate(image, left_template, cv2.TM_CCOEFF_NORMED)
    result_right = cv2.matchTemplate(image, right_template, cv2.TM_CCOEFF_NORMED)

    # 获取匹配结果的最大值
    min_val_left, max_val_left, min_loc_left, max_loc_left = cv2.minMaxLoc(result_left)
    min_val_right, max_val_right, min_loc_right, max_loc_right = cv2.minMaxLoc(result_right)

    # 根据匹配得分判断手指方向
    if max_val_left > max_val_right:
        return "Left"
    else:
        return "Right"


# 示例使用
image_path = '2.jpg'
left_template_path = 'left_template.jpg'
right_template_path = 'right_template.jpg'

direction = detect_glove_direction(image_path, left_template_path, right_template_path)
print(f"手套手指方向: {direction}")
